//
//  ViewController.swift
//  MKPhotoDetailDemo
//
//  Created by 劢克科技 on 2019/3/15.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    lazy var image: UIImageView = {
        let image = UIImageView()
        image.image = #imageLiteral(resourceName: "屏幕快照 2019-02-12 上午9.40.00")
        image.contentMode = .scaleAspectFill
        image.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        image.clipsToBounds = true
        image.isUserInteractionEnabled = true
        return image
    }()

    lazy var image2: UIImageView = {
        let image = UIImageView()
        image.image = #imageLiteral(resourceName: "屏幕快照 2019-02-01 下午5.29.42")
        image.contentMode = .scaleAspectFill
        image.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        image.clipsToBounds = true
        image.isUserInteractionEnabled = true
        return image
    }()
    
    //MARK: - Life Circle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        setUI()
    }

    fileprivate func setUI() {
        view.addSubview(image)
        image.frame = CGRect(x: (UIScreen.main.bounds.width - image.frame.width) / 2, y: (UIScreen.main.bounds.height - image.frame.height) / 2, width: image.frame.width, height: image.frame.height)
        let imageTap = UITapGestureRecognizer(target: self, action: #selector(handleImageClick(gesture:)))
        image.addGestureRecognizer(imageTap)
        
        view.addSubview(image2)
        image2.frame = image.frame
        image2.center = CGPoint(x: 100, y: 100)
        let imageTap2 = UITapGestureRecognizer(target: self, action: #selector(handleImageClick2(gesture:)))
        image2.addGestureRecognizer(imageTap2)
    }
    
    //MARK: - 自定义事件
    
    // 点击图片
    // present图片查看器
    @objc func handleImageClick(gesture: UITapGestureRecognizer) {
        let controller = MKPhotosViewController()
        controller.modalPresentationStyle = .custom
        controller.image = image
        print(image.frame)
        print(image.superview!.convert(image.frame, to: UIApplication.shared.keyWindow))
        present(controller, animated: false, completion: nil)
    }
    
    @objc func handleImageClick2(gesture: UITapGestureRecognizer) {
        let controller = MKPhotosViewController()
        controller.modalPresentationStyle = .custom
        controller.image = image2
        present(controller, animated: false, completion: nil)
    }
}

